package com.dahua.messagesyncbj.runner;

import com.dahua.messagesyncbj.business.Constants;
import com.dahua.messagesyncbj.config.redis.RedisUtil;
import com.dahua.messagesyncbj.criteria.EquipmentInfoQueryCriteria;
import com.dahua.messagesyncbj.criteria.StatusQueryCriteria;
import com.dahua.messagesyncbj.entity.EquipmentChange;
import com.dahua.messagesyncbj.entity.Status;
import com.dahua.messagesyncbj.mapper.EquipmentMapper;
import com.dahua.messagesyncbj.mapper.FaultMapper;
import com.dahua.messagesyncbj.pojo.EquipmentInfo;
import com.dahua.messagesyncbj.service.EquipmentChangeService;
import com.dahua.messagesyncbj.service.EquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

import static com.dahua.messagesyncbj.business.Constants.EQUIPMENT_HASH;
import static com.dahua.messagesyncbj.business.Constants.LATEST_STATUS_HASH;

@Slf4j
@Component
public class SyncScheduleRunner implements Runnable {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private FaultMapper faultMapper;
    @Resource
    private EquipmentMapper equipmentMapper;
    @Resource
    private EquipmentService equipmentService;
    @Resource
    private EquipmentChangeService equipmentChangeService;

    @Override
    public void run() {
        while (true) {
            try {
                this.handleEquipmentSync();
                Thread.sleep(5000);
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
    }

    @Transactional
    public void handleEquipmentSync() {
        List<EquipmentChange> list = equipmentChangeService.listUnhandled();
        for (EquipmentChange equipmentChange : list) {

            EquipmentInfoQueryCriteria infoCriteria = new EquipmentInfoQueryCriteria();
            infoCriteria.setId(equipmentChange.getEquipmentId());

            switch (equipmentChange.getActionType()) {
                case "create": {
                    EquipmentInfo info = equipmentMapper.enquire(infoCriteria);
                    if (info == null) {
                        equipmentChange.setBk1("equipment don't exist");
                        equipmentChangeService.handle(equipmentChange);
                        continue;
                    }

                    Status status = Status.builder().
                            lineId(info.getLineId())
                            .stationId(info.getStationId())
                            .equipmentId(info.getId())
                            .manufactureId(info.getName())
                            .equipmentType(info.getBk2())
                            .logTime(Timestamp.valueOf(LocalDateTime.now().minusDays(1L)))
                            .build();
                    /*if (MACHINE_TYPES.contains(info.getBk2())) {
                        status.setIsMachine(1);
                    } else {
                        status.setIsMachine(0);
                    }*/
                    equipmentService.insertLatestStatus(status);
                    break;
                }
                case "change": {
                    EquipmentInfo info = equipmentMapper.enquire(infoCriteria);
                    if (info == null) {
                        equipmentChange.setBk1("equipment don't exist");
                        equipmentChangeService.handle(equipmentChange);
                        continue;
                    }

                    redisUtil.setHash(Constants.EQUIPMENT_HASH, info.getName(), info);

                    /*Status status = new Status();

                    status.setEquipmentId(equipmentChange.getEquipmentId());
                    equipmentService.deleteLatestStatus(Collections.singletonList(status));

                    status.setLineId(info.getLineId());
                    status.setStationId(info.getStationId());
                    status.setEquipmentId(info.getId());
                    status.setManufactureId(info.getName());
                    status.setEquipmentType(info.getBk2());
                    status.setLogTime(Timestamp.valueOf(LocalDateTime.now().minusDays(1L)));
                    *//*if (MACHINE_TYPES.contains(info.getBk2())) {
                        status.setIsMachine(1);
                    } else {
                        status.setIsMachine(0);
                    }*//*
                    equipmentService.insertLatestStatus(status);*/
                    List<Status> statuses = equipmentService.listLatestStatus(new StatusQueryCriteria(info.getName()));
                    if (statuses != null && !statuses.isEmpty()) {
                        Status status = statuses.get(0);
                        status.setLineId(info.getLineId());
                        status.setStationId(info.getStationId());
                        status.setEquipmentId(info.getId());
                        status.setManufactureId(info.getName());
                        status.setEquipmentType(info.getBk2());
                        equipmentService.updateLatestStatus(status);
                    } else {

                        Status status = Status.builder().
                                lineId(info.getLineId())
                                .stationId(info.getStationId())
                                .equipmentId(info.getId())
                                .manufactureId(info.getName())
                                .equipmentType(info.getBk2())
                                .logTime(Timestamp.valueOf(LocalDateTime.now().minusDays(1L)))
                                .build();
                        equipmentService.insertLatestStatus(status);
                    }
                    faultMapper.modifyFaultInfo(info);
                    break;
                }
                case "delete": {
                    EquipmentInfo info = new EquipmentInfo();
                    info.setId(equipmentChange.getEquipmentId());

                    faultMapper.deleteFaultInfo(info);

                    /*  删除缓存*/
                    /*Status status = new Status();
                    status.setEquipmentId(equipmentChange.getEquipmentId());
                    equipmentService.deleteLatestStatus(Collections.singletonList(status));
                    EquipmentInfo enquire = equipmentMapper.enquire(infoCriteria);
                    if (enquire == null) {
                        equipmentChange.setBk1("equipment don't exist");
                        equipmentChangeService.handle(equipmentChange);
                        continue;
                    }*/
                    List<Status> statuses = equipmentService.listLatestStatus(new StatusQueryCriteria(equipmentChange.getManufactureId()));
                    if (statuses != null && !statuses.isEmpty()) {
                        equipmentService.deleteLatestStatus(statuses);
                        redisUtil.deleteHash(LATEST_STATUS_HASH, equipmentChange.getManufactureId());
                        redisUtil.deleteHash(EQUIPMENT_HASH, equipmentChange.getManufactureId());
                    }
                    break;
                }
            }

            equipmentChangeService.handle(equipmentChange);
        }
    }
}
